package demoMod.invincibleOne.cards.invincible;

import basemod.abstracts.CustomSavable;
import basemod.helpers.CardModifierManager;
import com.badlogic.gdx.math.Vector2;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.SpawnMonsterAction;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.monsters.exordium.AcidSlime_M;
import com.megacrit.cardcrawl.monsters.exordium.AcidSlime_S;
import com.megacrit.cardcrawl.monsters.exordium.SpikeSlime_M;
import com.megacrit.cardcrawl.monsters.exordium.SpikeSlime_S;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.rooms.RestRoom;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.EnterRoomHook;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;
import demoMod.invincibleOne.relics.TaskGoal;

public class Ambush extends AbstractInvincibleCard implements EnterRoomHook, CustomSavable<Integer> {
    public static final String ID = InvincibleOneMod.makeID("Ambush");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Ambush.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.NONE;
    private static final Vector2 vector2 = new Vector2(120.0F * Settings.scale, 0.0F);

    private static final int COST = -2;

    private int n = 0;

    public Ambush() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
        this.tags.add(CardTagsEnum.ALERT);
        this.rawDescription = String.format(cardStrings.DESCRIPTION, n);
        this.initializeDescription();
    }

    @Override
    public boolean onBattleStart() {
        TaskGoal.AddToAlertQueue(1, new TaskGoal.AlertLambda() {
            @Override
            public void run() {
                if (n > 0) {
                    n--;
                    AbstractMonster slime;
                    if (upgraded) {
                        if (AbstractDungeon.cardRandomRng.randomBoolean()) {
                            slime = new AcidSlime_M(0.0F, 0.0F);
                        } else {
                            slime = new SpikeSlime_M(0.0F, 0.0F);
                        }
                        rawDescription = String.format(cardStrings.UPGRADE_DESCRIPTION, n);
                    } else {
                        if (AbstractDungeon.cardRandomRng.randomBoolean()) {
                            slime = new AcidSlime_S(0.0F, 0.0F, 0);
                        } else {
                            slime = new SpikeSlime_S(0.0F, 0.0F, 0);
                        }
                        rawDescription = String.format(cardStrings.DESCRIPTION, n);
                    }
                    initializeDescription();
                    slime.drawX = Settings.WIDTH / 2.0F - vector2.x;
                    slime.drawY = Settings.HEIGHT / 2.0F - vector2.y;
                    addToBot(new SpawnMonsterAction(slime, true));
                    addToBot(new AbstractGameAction() {
                        @Override
                        public void update() {
                            slime.createIntent();
                            isDone = true;
                        }
                    });
                    vector2.rotate(45.0F);
                }
            }
        });
        return n >= 0;
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = String.format(cardStrings.UPGRADE_DESCRIPTION, n);
            this.initializeDescription();
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void onEnterRoom(AbstractRoom room) {
        if (room instanceof RestRoom) {
            if (this.misc < 0) {
                this.misc = 0;
            }
            this.misc++;
            n = misc;
            this.rawDescription = String.format(upgraded ? cardStrings.UPGRADE_DESCRIPTION : cardStrings.DESCRIPTION, n);
            this.initializeDescription();
        }
    }

    @Override
    public Integer onSave() {
        return n;
    }

    @Override
    public void onLoad(Integer i) {
        if (i != null) {
            n = i;
        }
        this.rawDescription = String.format(upgraded ? cardStrings.UPGRADE_DESCRIPTION : cardStrings.DESCRIPTION, n);
        this.initializeDescription();
    }
}
